package com.woniuxy01;

import java.sql.*;
import java.util.Scanner;

/**
 * @author 作者
 * @create 2025-06-16 15:11
 */
public class Test02 {
    public static void main(String[] args) throws Exception {
        test03();
    }

    private static void test03() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        //执行添加操作，准备sql语句
        System.out.println("输入姓名");
        String name = scanner.nextLine();
        System.out.println("输入密码");
        String pwd = scanner.nextLine();
        //列值是用?做占位符
        String sql="SELECT * FROM student WHERE NAME=? AND PASSWORD=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,name);
        preparedStatement.setString(2,pwd);
        System.out.println("sql="+sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        //元数据：描述数据的数据
        ResultSetMetaData metaData = resultSet.getMetaData();
        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            String columnName = metaData.getColumnName(i);
            System.out.print(columnName+"\t");
        }

        //判断是否存在下一条数据，同时取到下一条数据
        if(resultSet.next()){

            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }


        resultSet.close();
        preparedStatement.close();
        connection.close();
    }


    private static void test02() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        //执行添加操作，准备sql语句
        System.out.println("输入姓名");
        String name = scanner.nextLine();
        System.out.println("输入密码");
        String pwd = scanner.nextLine();
        String sql="SELECT * FROM student WHERE NAME='"+name+"' AND PASSWORD='"+pwd+"'";
        System.out.println(sql);
        Statement statement = connection.createStatement();
        //执行查询返回ResultSet，对象封装查询结果
        ResultSet resultSet = statement.executeQuery(sql);
        //判断是否存在下一条数据，同时取到下一条数据
        if(resultSet.next()){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
        resultSet.close();
        statement.close();
        connection.close();
    }



    private static void test01() throws ClassNotFoundException, SQLException {
        Scanner scanner=new Scanner(System.in);
        String className="com.mysql.cj.jdbc.Driver";
        String url="jdbc:mysql:///db1";
        String user="root";
        String password="123";
        Class.forName(className);
        Connection connection = DriverManager.getConnection(url, user, password);
        //执行添加操作，准备sql语句
        System.out.println("输入姓名");
        String sname = scanner.next();
        String sql="select * from stu where sname like '%"+sname+"%'";
        Statement statement = connection.createStatement();
        //执行查询返回ResultSet，对象封装查询结果
        ResultSet resultSet = statement.executeQuery(sql);
        //判断是否存在下一条数据，同时取到下一条数据
        while(resultSet.next()){
            System.out.println(resultSet.getInt(1));
            System.out.println(resultSet.getString("sname"));
            System.out.println(resultSet.getInt(3));
        }
        resultSet.close();
        statement.close();
        connection.close();
    }

}

